<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" plain icon="el-icon-refresh" @click="tableRefresh()"></el-button>
				<el-button type="primary" @click="showDialog(0)">新增</el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">

				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTableGdshop ref="table" tableName="goodsTable" :apiObj="apiObj" stripe remoteSort remoteFilter
						   :params="tableParams" :column="tableColumn">
				<template #type_id="scope">
					{{ getPageType(scope.row.type_id)}}
				</template>

				<template #status="scope">
					{{ getStatus(scope.row.status) }}
				</template>

				<el-table-column label="操作" fixed="right" align="center" width="100">
					<template #default="scope">
						<el-button type="text" size="small" @click="editItem(scope.row.id)">编辑</el-button>
						<el-button type="text" size="small" @click="delItem(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</scTableGdshop>
		</el-main>
	</el-container>
	<form-diy ref="formDiy" @success="formDiySuccess" :baseUrl="baseUrl" />
</template>

<script>
import useTabs from '@/utils/useTabs'
import FormDiy from "./formDiy";

export default {
	name: "diy-page-item-list",
	components: {FormDiy},
	data() {
		return {
			apiObj: this.$API.shop.diyPageItem,
			pageTypeArray: {},
			tableParams: {
				page_id: 0,
			},
			baseUrl: '',
			tableColumn: [
				{
					prop: "title",
					label: "标题",
					align: "center"
				},
				{
					prop: "type_id",
					label: "组件类型",
					align: "center"
				},
				{
					prop: "sort",
					label: "排序",
					align: "center",
				},
				{
					prop: "status",
					label: "状态",
					align: "center",
				},
			],
		}
	},
	beforeMount() {
		this.tableParams.page_id = Number(this.$route.query.page_id);
	},
	mounted() {
		this.getBaseUrl();
		this.getDiyPageType();
	},
	methods: {
		async getBaseUrl() {
			this.baseUrl = await this.$TOOL.getBaseUrl(this.$API)
			console.log(this.baseUrl)
		},
		getDiyPageType() {
			this.$API.shop.diyPageType.list().then(res => {
				let tmp = {};
				res.data.map((item) => {
					if (item.parent_id > 0) {
						tmp[item.id] = item.type_name;
					}
				});
				this.pageTypeArray = tmp
			})
		},
		getPageType(val) {
			return this.pageTypeArray[val];
		},
		getStatus(val) {
			if (val === 1) {
				return '启用';
			}

			return '禁用';
		},
		tableRefresh() {
			this.$refs.table.refresh();
		},
		showDialog(id) {
			this.$refs.formDiy.showDialog(this.tableParams.page_id,id);
		},
		formDiySuccess() {
			this.tableRefresh();
		},
		editItem(_id) {
			this.showDialog(_id);
		},
		delItem(row) {
			this.$confirm(`确定删除选中的 ${row.title}  吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const loading = this.$loading();
				var res = await this.apiObj.delete({ ids: [row.id] });
				loading.close();
				if (res.code === 0) {
					this.$message.success("操作成功")
					this.tableRefresh()
				}
				else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(() => {

			})
		}
	},
	//路由跳转进来 判断from是否有特殊标识做特殊处理
	beforeRouteEnter (to, from, next){
		next((vm)=>{
			console.log(vm);
			if(from.title){
				//执行特殊方法
				useTabs.setTitle(from.title)
				//删除特殊标识，防止标签刷新重复执行
				delete from.title
			}
		})
	},
}
</script>

<style scoped>

</style>
